﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core.DAL.SubSonicDAL;
using Medianamik.Core.Profile;
using Medianamik.Core.Querying;
using Medianamik.Core;
using Medianamik.UI.Blog.Logic;
using Medianamik.UI.Blog.NodeTypes;
using Medianamik.UI.Controls;
using Telerik.Web.UI;
using System.Web.Security;
using Medianamik.Core.Security;
using Node = Medianamik.Core.Node;


namespace Medianamik.UI.Blog
{
    public partial class userList : BaseModuleListPage
    {





        protected override void GridNeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            var users = SecurityManager.GetUsersInGroups(FilteredGroups);
            IEnumerable<ProfileNode> profiles;

            if (users.Count() > 0)
               profiles = new Query()
                    //.Where(n => n.ParentID, Comparison.Equal, RootNodeId)
                     .Where(n => n.NodeTypeID, Comparison.Equal, MedianamikTypes.MedianamikProfile)
                     .Where(MedianamikProperties.ProfileUser, Comparison.In, users)
                     .AsQuery<ProfileNode>().Execute();
            else
                profiles = new Query()
                    //.Where(n => n.ParentID, Comparison.Equal, RootNodeId)
                                 .Where(n => n.NodeTypeID, Comparison.Equal, MedianamikTypes.MedianamikProfile)
                                 .AsQuery<ProfileNode>().Execute();

            Grid.DataSource = profiles;
        }

        protected override string ReturnUrl
        {
            get { return "/medianamik/blog/userlist.aspx"; }
        }

        protected override Guid RootNodeId
        {
            get { return MedianamikModules.SecurityManagement; }
        }

        protected override RadGrid Grid
        {
            get { return ForumUserGrid; }
        }

        protected override UpdatePanel GridUpdatePanel
        {
            get { return ForumUserUpdatePanel; }
        }

        protected override string ManagementURL
        {
            get { return "editUser.aspx"; }
        }

        protected override GridOptions GridOptions
        {
            get
            {
                return GridOptions.None;
            }
        }

        protected virtual HashSet<String> FilteredGroups
        {
            get
            {
                return new HashSet<string>(new[] { "Blogger", "Commentator" });
            }
        }



        protected string GetUserRole(Guid userId)
        {
            var user = Membership.GetUser(userId);

            if (user == null)
                return "";

            return Core.Security.Roles.GetGroupsForUser(user.UserName).Contains("Blogger") ? "Blogger" : "Commentator";
        }

    }
}

